/**
 * Created by 83617 on 2019/1/11.
 */
var Society = require('../models/society')
var Activity = require('../models/activity')
var _ = require('underscore')

//Activity...
exports.listActivity = function(req,res){
    Activity.findByAggr(function(err,activities){
        if(err) console.log(err)
        //console.log(activities[0].full)
        res.render('listActivity',{
            title: '活动列表',
            activities:activities
        })
    })
    //Activity.fetch(function(err,activities){
    //    if(err) console.log(err)
    //    res.render('listActivity',{
    //        activities:activities
    //    })
    //})
}

exports.toActivityInsertPage = function(req,res){
    Society.findSingleCol('name',function(err,societies){
        if(err) console.log(err)
        //console.log(societies)
        res.render('adminActivity',{
            title: '活动录入',
            societies:societies,
            activity: {
                name:'',
                society:'',
                detail:''
            }
        })
    })
}

exports.updateActivityFunc = function(req,res){
    var activityObj = req.body.activity
    var b = ''
    //注意callback()的执行顺序啊！！！被坑惨了，node是异步执行！
    Society.findIdByName(activityObj.society,function(err,societyId){
        if(err) console.log(err)
        //console.log(societyId._id)
        b = societyId._id

        var _activity = new Activity({
            name: activityObj.name,
            theSociety: activityObj.society,
            detail: activityObj.detail,
            societyId: b
        })
        //res.redirect('/admin/activity')
        _activity.save(function(err,activity){
            if(err) console.log(err)
            res.redirect('/admin/activity')
        })
    })
}